<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Expires" content="0" />

<meta name="_csrf" th:content="${_csrf.token}" />
<meta name="_csrf_header" th:content="${_csrf.headerName}" />

<link rel="stylesheet" media="screen" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}"/>
<link rel="stylesheet" media="screen" th:href="@{/webjars/bootstrap-table/dist/bootstrap-table.min.css}">

<style type="text/css">
body{
	position:absolute;
	width:100%;
	height:100%;
	margin: 0;
	padding: 0;
	font: 12px tahoma, Arial, Verdana, sans-serif;
	color: #666666;
}
.buttonMouseOut{
	width: 125px;
    height: 34px;
    line-height: 34px;
    text-align: center;
    outline: none;
    cursor: pointer;
    border: none;
    background: url(../../../images/install.png) -4px -3px no-repeat;
    font: 12px tahoma, Arial, Verdana, sans-serif;
	color: #666666;
}

.buttonMouseOver{
	width: 125px;
    height: 34px;
    line-height: 34px;
    text-align: center;
    outline: none;
    cursor: pointer;
    border: none;
    background: url(../../../images/install.png) -4px -64px no-repeat;
    font: 12px tahoma, Arial, Verdana, sans-serif;
	color: #666666;
}


.uploadPlugin input:focus { 
    outline: none;
    background-color: transparent;
}
.uploadPlugin input::selection{background:transparent; }
.uploadPlugin input::-moz-selection{background:transparent; }


</style>

<title>代码生成器</title>

</head>
<body>
	<div class="error" style="padding: 10px;height: auto;display: none;">
		<div class="alert alert-warning" style="display: none;">
            <a href="#" class="close" data-dismiss="alert">
		        &times;
		    </a>
			<strong>错误！</strong><span class="alertMsg" style="display: inline;"></span>
        </div>
	</div>
	<div class="success" style="padding: 10px;height: 72px;display: none;">
        <div id="success" class="alert alert-success">
		    <a href="#" class="close" data-dismiss="alert">&times;</a>
		    <strong>提示！</strong><span class="successMsg" style="display: inline;"></span>
		</div>
	</div>
	<div style="padding: 10px;">
		<div class="uploadPlugin">
			<form id="uploadPluginForm">
				<div style="height: 38px;">
					<div style="width: 200px;float: left;">
						<select style="height: 38px;" name="groupName" class="form-control">
							<option value="">----</option>
							<option th:each="groupName: ${groupNameList}" th:value="${groupName}" th:text="${groupName}"></option>
						</select>
					</div>
					<div style="height: 38px;float: left;">
						<div style="margin-left: 10px;padding: 0px 5px;border: 1px solid rgba(0,0,0,.15);height: 38px;line-height:38px;width:180px;vertical-align:middle;float: left;border-top-left-radius: 5px;border-bottom-left-radius: 5px;">
							<span><input class="fileName" type="text" style="height: 20px;width: 100%;border: 0px;font-family: inherit;font-size: inherit;line-height: inherit;" onkeydown="return false;" onpaste="return false;" placeholder="选择文件" title="选择文件"/></span>
						</div>
						<div style="float: right;height: 38px;width: 120px;border-top-left-radius: 0;border-bottom-left-radius: 0;" tabindex="500" class="btn btn-primary btn-file chooseFile">
							<i class="glyphicon glyphicon-folder-open"></i>&nbsp;  
							<span>选择文件</span>
						</div>
						<input style="opacity: 0;top: 0;left:0;width: 0;height: 0;position: fixed;" name="file" class="file" type="file" accept=".jar">
					</div>
					<div style="float: left;margin-left: 10px;">
						<button type="button" id="uploadPlugin" class="buttonMouseOut" onmousemove="this.className='buttonMouseOver'"onmouseout="this.className='buttonMouseOut'">上传插件</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div style="padding: 10px;">
		<table id="pluginTable"></table>
	</div>
</body>

<script type="text/javascript" th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/webjars/jquery-form/jquery.form.min.js}"></script>
<script type="text/javascript" th:src="@{/webjars/bootstrap-table/dist/bootstrap-table.min.js}"></script>
<script type="text/javascript" th:src="@{/webjars/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js}"></script>

<script type="text/javascript">
	$(document).ready(function(){
		$('#pluginTable').bootstrapTable({
			url: "/init/wizard/pluginList?pluginGroupName=" + ($("select[name='groupName']").val() ? $("select[name='groupName']").val() : ''),
		    columns: [
		    	{field: '',title: '序号',formatter : function(value, row, index){return index + 1;}},
				{field: 'groupName',title: '插件组名'},
				{field: 'name',title: '插件名称'},
		    	{field: 'description',title: '插件描述'}, 
		    	{field: 'dependencies',title: '插件依赖'},
		    	{field: 'status',title: '禁用',formatter : function(value, row, index){
				        	return value == 0 ? "<input type='button' _status='" + row.status + "' id='" + row.id + "' value='禁用'/>" : "<input type='button' _status='" + row.status + "' id='" + row.id + "' value='启用'/>";
				        }
		    	},
		    ],
		    onLoadSuccess: function(rows){
		    	$("table#pluginTable input[type='button']").each(function(){
					var button = $(this);
					button.click(function(){
						$("div.error,div.success").hide();
						
						//启用
						if($(this).attr("_status") == 1){
							$.post("/init/wizard/enablePlugin/" + $(this).attr("id"),function(data){
								if(data && data.errorMessage){
									$("span.alertMsg").text(data.errorMessage);
									$("div.error,div.alert").show();
								}else{
									$("span.successMsg").text("启用成功");
									$("div.success,div.alert").show();
								}
								$('#pluginTable').bootstrapTable('refresh', {silent: true});
							});
						}else{
							//禁用
							$.post("/init/wizard/disablePlugin/" + $(this).attr("id"),function(data){
								if(data && data.errorMessage){
									$("span.alertMsg").text(data.errorMessage);
									$("div.error,div.alert").show();
								}else{
									$("span.successMsg").text("禁用成功");
									$("div.success,div.alert").show();
								}
								$('#pluginTable').bootstrapTable('refresh', {silent: true});
							});
						}
					});
				});
		    	
		    	//鼠标放上去增加提示信息
		    	$("table#pluginTable tbody tr").each(function(index){
		    		var row = rows[index];
		    		if(row){
						var title = "插件名称：" + row.name + "\r\n";
						title = title + "插件介绍：" + row.description + "\r\n";
						title = title + "插件模板文件路径：" + row.templatePath + "\r\n";
						title = title + "插件生成器全路径名：" + row.generator + "\r\n";
						title = title + "代码相对目录：\/" + row.fileRelativeDir + "\/"+ row.prefix + "文件名" + row.suffix + row.fileSuffix + "\r\n";
						title = title + "插件地址：" + row.pluginPath + "\r\n";
						title = title + "依赖的插件：" + row.dependencies + "\r\n";
						title = title + "状态：" + (row.status == 0 ? "启用" : "禁用") + "\r\n";

						$(this).attr("title",title);
					}
		    	});
		    }
		});

		$("select[name='groupName']").change(function () {
			var groupName = $(this).val();
			$('#pluginTable').bootstrapTable('refresh', {silent: true, url: "/init/wizard/pluginList?pluginGroupName=" + groupName + "&t=" + (new Date()).getTime()});
		});

		//上传插件
		$(".chooseFile").click(function(){
			$("input[type='file'].file").trigger("click");
		});
		$("input[type='file'].file").change(function(){
			var val = $(this).val();
			var fileName = null;
			if(val){
				fileName = val.substring(val.lastIndexOf("\\") + 1);
			}else{
				fileName = "选择文件";
			}
			
			$("input.fileName").attr("placeholder",fileName).attr("title",fileName);
		});
		
		$("button#uploadPlugin").click(function(){
			$("div.success,div.alert").hide();
			//校验是否选择插件文件
			var pluginName = $("input[type='file'].file").val();
			if(!pluginName){
				$("span.alertMsg").text("请先选择插件");
				$("div.error,div.alert").show();
				return;
			}
			//校验插件文件是否以.jar结尾
			if(pluginName.substring(pluginName.length - 4) != '.jar'){
				$("span.alertMsg").text("插件文件名必须以.jar结尾");
				$("div.error,div.alert").show();
				return;
			}
			
			$("div.error,div.alert").hide();
			
			//上传插件
			$("form#uploadPluginForm").ajaxSubmit({
				method: "post",
				url: "/init/wizard/uploadPlugin",
				success: function(data){
					//上传失败提示原因
					if(data && data.errorMessage){
						$("span.alertMsg").text("上传插件失败：" + data.errorMessage);
						$("div.error,div.alert").show();
					}else{
						if(data){
							$("span.successMsg").text("上传插件成功");
							$("div.success,div.alert").show();
							
							//上传成功提示上传成功并刷新插件列表
							document.getElementById("uploadPluginForm").reset();
							
							$("input.fileName").attr("placeholder","请先选择插件").attr("title","请先选择插件");
							
							//重新加载插件
							$("select[name='groupName']").val("").trigger("change");
						}else{
							$("span.alertMsg").text("上传插件失败");
							$("div.error,div.alert").show();
						}
					}
				},
				error: function(context){
					//上传失败提示原因
					$("span.alertMsg").text("上传插件失败：" + context.responseJSON.message);
					$("div.error,div.alert").show();
				}
			});
			
			
		});
	});
	
function valid(){ return true; }
function save(){}
</script>
</html>
